// CGPS.h : Declaration of the CCGPS

#ifndef __CGPS_H_
#define __CGPS_H_

#include "resource.h"       // main symbols
#include "Subclass.h"
#include <afxtempl.h>
#include "../include/comhdr.h"

// {BE65B70F-F7B1-42ca-9F3F-D95F2ECEC599}
/////////////////////////////////////////////////////////////////////////////
// CCGPS
class ATL_NO_VTABLE CCGPS : 
	public CComObjectRootEx<CComSingleThreadModel>,
	public CComCoClass<CCGPS, &CLSID_CGPS>,
	public ISupportErrorInfo,
	public IConnectionPointContainerImpl<CCGPS>,
	public IDispatchImpl<ICGPS, &IID_ICGPS, &LIBID_ADDINGPSLib>,
	public IDispatchImpl<IFrameworkAddin, &IID_IFrameworkAddin, &LIBID_ADDINGPSLib>
{
public:
	CCGPS()
	{
	}

DECLARE_REGISTRY_RESOURCEID(IDR_CGPS)

DECLARE_PROTECT_FINAL_CONSTRUCT()

BEGIN_COM_MAP(CCGPS)
	COM_INTERFACE_ENTRY(ICGPS)
	COM_INTERFACE_ENTRY2(IDispatch,ICGPS)
	COM_INTERFACE_ENTRY(ISupportErrorInfo)
	COM_INTERFACE_ENTRY(IConnectionPointContainer)
	COM_INTERFACE_ENTRY(IFrameworkAddin)
END_COM_MAP()
BEGIN_CONNECTION_POINT_MAP(CCGPS)
END_CONNECTION_POINT_MAP()

BEGIN_CATEGORY_MAP(CCGPS)
IMPLEMENTED_CATEGORY(CATID_PROJECT_FRAMEWORK_ADDINS)
END_CATEGORY_MAP()

// ISupportsErrorInfo
	STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);

public:
	long m_lSessionID;
	static IFrameWork* m_pProjectFramework;
// ICGPS
public:
	STDMETHOD(ShowMacroIDE)();
	STDMETHOD(Initialize)(LONG lSessionID, IFrameWork * pApp, VARIANT bFirstTime)
	{
		AFX_MANAGE_STATE(AfxGetStaticModuleState())

		m_lSessionID = lSessionID;

		CString szModuleName,szModulePath;
		GetModuleFileName(AfxGetInstanceHandle(),szModuleName.GetBufferSetLength(MAX_PATH),MAX_PATH);
		szModuleName.ReleaseBuffer();
		int index = szModuleName.ReverseFind('\\');
		szModulePath = szModuleName.Left(index);
		
		_bstr_t message = L"Loading: " + szModuleName;
		pApp->LoggingService(message);

		CString XMLFileName;
		XMLFileName = szModuleName.Right(szModuleName.GetLength() - index - 1);
		XMLFileName = XMLFileName.Left(XMLFileName.ReverseFind('.'));
		XMLFileName = XMLFileName +  ".xml";
		
		CString szPath = szModulePath + "\\XML\\" + XMLFileName;

		CFile xmlfile;
		CFileException ex;
		if(!xmlfile.Open(szPath,CFile::modeRead,&ex))
		{
			TCHAR szError[1024];
			ex.GetErrorMessage(szError, 1024);
			NoticeBox(szError);
			return S_FALSE;
		}

		CString strXMLCommandSubFile;
		xmlfile.Read(strXMLCommandSubFile.GetBufferSetLength(xmlfile.GetLength()),xmlfile.GetLength());	
		strXMLCommandSubFile.ReleaseBuffer();

		strXMLCommandSubFile.Replace("\n",""); //Replace all new line characters
		strXMLCommandSubFile.Replace("\t",""); //Replace all tab characters

		HINSTANCE lAppHandle=AfxGetInstanceHandle();
		m_pProjectFramework=pApp;
		pApp->SetAddInInfo(lSessionID,(long)lAppHandle,strXMLCommandSubFile.AllocSysString(),IDR_TOOLBAR_ADDIN4); 
		return S_OK;
		
	}
	HRESULT _stdcall Uninitialize(LONG lSessionID);
	HRESULT _stdcall EndInitialize();
	static BOOL CALLBACK EnumSubingWndProc(HWND hwnd,LPARAM lParam);
	static CList<CSubclassWnd*,CSubclassWnd*> m_SubWndList;

};

#endif //__CGPS_H_
